#!/usr/bin/python


"""
File format:

Number of Cores
work period 
work period 
work period 
...
work period 

"""
import math
import sys
import os
import random
import shutil

if len(sys.argv) < 1:
    print "Usage: ./randSched numProcessors dirname"
    sys.exit(-1)

numCores = int(sys.argv[1])
dirname = sys.argv[2]

EPSILON = .001
MIN_WORK = 1
MIN_PERIOD = 1 + MIN_WORK
MAX_PERIOD = 10
MAX_WORK = MAX_PERIOD - 1

if not os.path.exists(dirname):
    print "Dirpath invalid"
    sys.exit(-1)
    
f = open(dirname + "/" + str(numCores) + "_cores", 'w')

totalRate = 0.0
lcm = 1

def gcdFunc ( a , b ) :
    if a == 0 or b == 0 :
        return max(a,b)
    while a != b :
        if a < b :
            b = b - a
        else :
            a = a - b
    return a

def lcmFunc ( a , b ) :
    g = gcdFunc( a , b )
    return (a/g) * b

f.write(str(numCores) + "\n")

while totalRate + EPSILON < numCores :
    work = 0.
    period = 0.
    while work == period:
        work = random.randint(MIN_WORK, MAX_WORK)
        period = random.randint(MIN_PERIOD, MAX_PERIOD)
    if work > period :
        temp = work
        work = period
        period = temp
    
    possTotalRate = totalRate + (float(work)/float(period))
    if possTotalRate > numCores:
        period = lcm
    	work = (period * (numCores - totalRate))
    	possTotalRate = totalRate + (float(work)/float(period))
    else :
        lcm = lcmFunc( lcm , period )

    f.write(str(work) + " " + str(period) + "\n")
    totalRate = possTotalRate   
    
f.close()
